import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

public class ResultSetTableModel extends AbstractTableModel {
	protected final ResultSet resultSet;
	private String[] columnNames;
	
	public ResultSetTableModel(ResultSet rs) {
		resultSet=rs;
		try {
			final ResultSetMetaData meta = resultSet.getMetaData();
			final int cc=meta.getColumnCount();
			columnNames=new String[cc];
			for(int i=0; i<cc; i++) {
				columnNames[i]=meta.getColumnName(i+1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	} 
	
	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}
	
	@Override
	public int getColumnCount() {
		int res=0;
		try {
			res=resultSet.getMetaData().getColumnCount();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public int getRowCount() {
		int res=0;
		try {
			resultSet.last();
			res=resultSet.getRow();
			resultSet.first();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public Object getValueAt(int row, int col) {
		Object obj=null;
		try {
			resultSet.absolute(row+1);
			obj=resultSet.getObject(col+1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//System.err.println(obj);
		return obj;
	}
}
